package com.drought.rained.permission;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.drought.rained.R;
import com.drought.rained.utils.ToastUtilsLJ;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

@SuppressLint("CheckResult")
public class PermissionLJ {
    private IPermissonLJ permisson;
    private Activity activity;

    public PermissionLJ(Activity activity, IPermissonLJ permisson) {
        this.activity = activity;
        this.permisson = permisson;
    }

    /**
     * 获取相关权限
     */
    public void requestAll() {
        RxPermissions rxPermissions = new RxPermissions(activity);
        final String[] permissions = {
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
        };
        rxPermissions.request(permissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    permisson.onSuccess(1);
                } else {
                    permissonError();
                }
            }
        });
    }

    //认证时相机权限
    public void authPermisson(final int type) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        final String[] permissions = {Manifest.permission.READ_CONTACTS};
        rxPermissions.request(permissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    permisson.onSuccess(type);
                } else {
                    permissonError();
                }
            }
        });
    }

    //认证时读取联系人权限
    public void contactPermisson(final int position) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        final String[] permissions = {Manifest.permission.CAMERA};
        rxPermissions.request(permissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    permisson.onSuccess(position);
                } else {
                    permissonError();
                }
            }
        });
    }

    //上传图片时相机权限
    public void uploadImage(final int position) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        final String[] permissions = {Manifest.permission.CAMERA};
        rxPermissions.request(permissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    permisson.onSuccess(position);
                } else {
                    permissonError();
                }
            }
        });
    }

    /**
     * 获取位置信息
     */
    public void uploadLocation() {
        RxPermissions rxPermissions = new RxPermissions(activity);
        String[] permissions = {Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        rxPermissions.request(permissions).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    permisson.onSuccess(0);
                } else {
                    permissonError();
                }
            }
        });
    }

    /**
     * 跳转到设置
     */
    private void jumpSettings() {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", activity.getPackageName());
        }
        activity.startActivity(intent);
    }

    /**
     * 权限获取不成功
     */
    private void permissonError() {
        ToastUtilsLJ.showShort(activity, R.string.get_permission_failure_hint);
        jumpSettings();
    }
}
